<?php
/*
 * Copyright 2013 by Jerrick Hoang, Ivy Xing, Sam Roberts, James Cook, 
 * Johnny Coster, Judy Yang, Jackson Moniaga, Oliver Radwan, 
 * Maxwell Palmer, Nolan McNair, Taylor Talmage, and Allen Tucker. 
 * This program is part of RMH Homebase, which is free software.  It comes with 
 * absolutely no warranty. You can redistribute and/or modify it under the terms 
 * of the GNU General Public License as published by the Free Software Foundation
 * (see <http://www.gnu.org/licenses/ for more information).
 * 
 */

/**
 * @version March 18, 2014
 * @author Joseph Chang
 * change log: newly created dbStaff file and functions
 */
include_once('dbinfo.php');
include_once(dirname(__FILE__) . '/../domain/Staff.php');

function create_Staff() {
    connect();
    mysql_query("DROP TABLE IF EXISTS dbClient");
    $result = mysql_query("CREATE TABLE dbClient(user_name TEXT NOT NULL, first_name TEXT NOT NULL, last_name TEXT, password TEXT)");
    if (!$result)
        echo mysql_error() . "Error creating dbStaff table<br>";
}

// add a staff to dbStaff table: if already there, return false 
function add_staff($staff) {
    if (!$staff instanceof Staff)
        die("Error: add_client type mismatch");
    
    connect();
    $query = "SELECT * FROM dbStaff WHERE user_name = '" . $staff_user->get_user_name() . "'";
    $result = mysql_query($query);
    
    if (!$result)
    {
        error_log('ERROR on select in add_staff '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    
    //if there's no entry for this id, add it
    if ($result == null || mysql_num_rows($result) == 0) {
        $insertRes = mysql_query('INSERT INTO dbClient VALUES("' .
                $person->get_user_name() . '","' .
                $person->get_first_name() . '","' .
                $person->get_last_name() . '","' .
                $person->get_password() . '","' . '");');
        if (!$insertRes) {
            error_log('ERROR on insert in add_staff ' . mysql_error());
            die('Invalid query: ' . mysql_error());
        }
        mysql_close();
        return true;
    }
    mysql_close();
    return false;
}

// remove a staff from dbStaff table.  If already there, return 
function remove_staff($userName) {
    connect();
    $query = 'SELECT * FROM dbStaff WHERE user_name = "' . $userName . '"';
    $result = mysql_query($query);
    if (!$result)
    {
        error_log('ERROR on select in remove_staff '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    if ($result == null || mysql_num_rows($result) == 0) {
        mysql_close();
        return false;
    }
    $query = 'DELETE FROM dbStaff WHERE user_name = "' . $userName . '"';
    $result = mysql_query($query);
    if (!$result)
    {
        error_log('ERROR on delete in add_staff '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    mysql_close();
    return true;
}

/*
 * @return a staff from dbStaff table matching a particular id.
 * if not in table, return false
 */
function retrieve_staff($userID) {
    connect();
    $query = "SELECT * FROM dbStaff WHERE user_name = '" . $userID . "'";
    $result = mysql_query($query);
    error_log("The query is: ".$query);
    if (!$result)
    {
        error_log('ERROR on select in retrieve_staff '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    if (mysql_num_rows($result) !== 1) {
        mysql_close();
        return false;
    }
    $result_row = mysql_fetch_assoc($result);
    // var_dump($result_row);
    $theStaff = make_a_staff($result_row);
    // mysql_close();
    return $theStaff;
}

// Name is first concat with last name. (e.g.) 'Tom the Cat'
// return array of Staff
function retrieve_staff_by_name ($name) {
	$staff = array();
	if (!isset($name) || $name == "" || $name == null) return $staff;
	connect();
	$name = explode(" ", $name);
	$first_name = $name[0];
	$last_name = $name[1];
    $query = "SELECT * FROM dbStaff WHERE first_name = '" . $first_name . "' AND last_name = '". $last_name ."'";
    $result = mysql_query($query);
    if (!$result)
    {
        error_log('ERROR on select in retrieve_staff_by_name '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    while ($result_row = mysql_fetch_assoc($result)) {
        $the_staff = make_a_staff($result_row);
        $staff[] = $the_staff;
    }
    return $staff;
}

// change password
function change_password($userName, $newPass) {
    connect();
    $query = 'UPDATE dbStaff SET password = "' . $newPass . '" WHERE user_name = "' . $userName . '"';
    $result = mysql_query($query);
    if (!$result)
    {
        error_log('ERROR on update in change_password '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    mysql_close();
    return $result;
}

function make_a_staff($result_row) {
    $theStaff = new Staff(
                    $result_row['user_name'],
                    $result_row['first_name'],
                    $result_row['last_name'],
                    $result_row['password']);
    return $theStaff;
}

?>
